//
//  BDComplaintsViewController.m
//  BadBoy
//
//  Created by wen on 2017/6/13.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDComplaintsViewController.h"
#import "BDComplaintsTableViewCell.h"
#import "BDTextView.h"
#import "BDUserModel.h"

@interface BDComplaintsViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *datasource;
@property (nonatomic, strong) BDTextView *textView;
@property (nonatomic, strong) NSIndexPath *selectedIndexPath;
@property (nonatomic, strong) UIButton *tousuButton;

@end

@implementation BDComplaintsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = ASLocalizedString(@"投诉");
    [self requestDatasource];
    [self.tableView reloadData];
    
    [[self.tousuButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (self.selectedIndexPath == nil) {
            [[UIApplication sharedApplication].keyWindow alertWithTitle:ASLocalizedString(@"提示")message:ASLocalizedString(@"请先选择投诉原因")type:AlertTypeAlert cancleTitle:ASLocalizedString(@"确定")otherTitle:nil clickBlock:^(NSInteger index) {

            }];
            return;
        }
        [MBManager showLoadingAlert:ASLocalizedString(@"请稍后")clickHidden:NO clickBlock:nil];
        NSString *url = [NSString stringWithFormat:@"%@users/%@/report", KBDBaseURL, self.user_id];
        
        NSString *reason = self.datasource[self.selectedIndexPath.row];
        if (self.textView.text.length) {
            reason = [[self.datasource[self.selectedIndexPath.row] stringByAppendingString:@":"] stringByAppendingString:self.textView.text];
        }
        NSDictionary *para = @{@"id":self.user_id,
                               @"reason":reason};
        NSString *headValid = [BDUserModel getHeaderValidation];
        RACSignal *signal = [[BDNetworkingServer yy_POST:url parameters:para validation:headValid automaticProcessingError:YES] execute:nil];
        [signal subscribeNext:^(id  _Nullable x) {
            [MBManager hideAlert];
            [[UIApplication sharedApplication].keyWindow alertWithTitle:ASLocalizedString(@"提示")message:ASLocalizedString(@"投诉成功")type:AlertTypeAlert cancleTitle:ASLocalizedString(@"确定")otherTitle:nil clickBlock:^(NSInteger index) {
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }];
        [signal subscribeError:^(NSError * _Nullable error) {
//            [MBManager hideAlert];
            [MBManager showBriefAlert:ASLocalizedString(@"投诉失败，请稍后再试")clickHidden:YES clickBlock:nil];
        }];
    }];
}

- (void)requestDatasource{
    self.datasource = @[ASLocalizedString(@"发布不适当内容对我造成骚扰"),
                        ASLocalizedString(@"存在欺诈骗钱行为"),
                        ASLocalizedString(@"此账号可能被盗用了"),
                        ASLocalizedString(@"存在侵权行为"),
                        ASLocalizedString(@"发布仿冒品信息"),
                        ASLocalizedString(@"其他")].mutableCopy;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datasource.count + 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row < self.datasource.count) {
        return 45;
    }else{
        return 140;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row < self.datasource.count) {
        BDComplaintsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"complaintsCell" forIndexPath:indexPath];
        cell.textLabel.text = self.datasource[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.is_Selected = NO;
        if (self.selectedIndexPath != nil) {
            if (indexPath.row == self.selectedIndexPath.row) {
                cell.is_Selected = YES;
            }
        }
        return cell;
    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"textViewCell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"textViewCell"];
            self.textView = [[BDTextView alloc] initWithFrame:CGRectMake(10, 1, kBDScreenWidth - 20, 130)];
            _textView.placeholder = ASLocalizedString(@"请输入投诉原因…");
            _textView.placeHolderFont = [UIFont systemFontOfSize:13];
            [cell.contentView addSubview:_textView];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row < self.datasource.count) {
        self.selectedIndexPath = indexPath;
        [self.tableView reloadData];
    }
}

- (UITableView *)tableView{
    if (!_tableView) {
        UITableViewController* tvc=[[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
        [self addChildViewController:tvc];
        [tvc.view setFrame:CGRectMake(0, kBDTopHeight, kBDScreenWidth, kBDScreenHeight - kBDTopHeight)];
        self.tableView = tvc.tableView;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[BDComplaintsTableViewCell class] forCellReuseIdentifier:@"complaintsCell"];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorInset = UIEdgeInsetsZero;
        _tableView.tableHeaderView = [self customTableHeaderView];
        _tableView.tableFooterView = [self customTableFooterView];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (UIView *)customTableHeaderView{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kBDScreenWidth, 40)];
    headView.backgroundColor = [UIColor clearColor];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = ASLocalizedString(@"请选择投诉该账号的原因：");
    label.textColor = [UIColor colorWithHexString:@"#808080"];
    label.font = [UIFont systemFontOfSize:15];
    [label sizeToFit];
    label.left = 15;
    label.centerY = headView.height / 2.0;
    
    [headView addSubview:label];
    
    return headView;
}

- (UIView *)customTableFooterView{
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kBDScreenWidth, 30)];
    
    UIButton *xuzhiButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [xuzhiButton setTitle:ASLocalizedString(@"投诉须知 >>")forState:UIControlStateNormal];
    [xuzhiButton setTitleColor:KBDColorBlue forState:UIControlStateNormal];
    xuzhiButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [xuzhiButton sizeToFit];
    xuzhiButton.left = 15;
    xuzhiButton.top = 15;
    [footerView addSubview:xuzhiButton];
    
    return footerView;
}

- (UIButton *)tousuButton{
    if (!_tousuButton) {
        self.tousuButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_tousuButton setTitle:ASLocalizedString(@"投诉")forState:UIControlStateNormal];
        [_tousuButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _tousuButton.backgroundColor = KBDColorBlue;
        _tousuButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _tousuButton.size = CGSizeMake(kBDScreenWidth - 20, 40);
        _tousuButton.bottom = kBDScreenHeight - 10;
        _tousuButton.left = 10;
        _tousuButton.layer.masksToBounds = YES;
        _tousuButton.layer.cornerRadius = 4;
        [self.view addSubview:_tousuButton];
    }
    return _tousuButton;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
